package com.example.demo.threadDemo.aQuQian;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description Account
 * @Author songchuanlong
 * @Date 2025-05-12
 */
public class Account {
    private double money;
    private String idCard;
    private final Lock lk =new ReentrantLock();

    public Account(double money, String idCard) {
        this.money = money;
        this.idCard = idCard;
    }

    @Override
    public String toString() {
        return "Account{" +
                "money=" + money +
                ", idCard='" + idCard + '\'' +
                '}';
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public void quQian(double money) {
        String name = Thread.currentThread().getName();
        try {
            lk.lock();
            if(this.money>=money){
                System.out.println("余额还剩"+this.money);
                this.money-=money;
                System.out.println(name+"取完"+money+"还剩"+this.money);
            }else {
                System.out.println("您的余额不足！");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lk.unlock();
        }
    }
}
